Ext.namespace('SLM');

SLM.DwcPanel = function(config) {

	this.my_species = false;

	this.grid = new SLM.DwcGrid();

	this.grid.on('beforeload', function( store, o, c, d) {
		o.params = { filters: Ext.encode( this.frm.getForm().getValues() ) }
	}, this);
	this.grid.store.on('beforeload', function( store, o ) {
		var params = {
			userFilter: Ext.getCmp('my_species').pressed
		}
		Ext.apply(o.params, params );
	});
	this.matrix = new SLM.DwcMatrix();
	this.matrix.store.on('beforeload', function( store, o ) {
		var params = {
			userFilter: Ext.getCmp('my_species').pressed
		}
		Ext.apply(o.params, params );
	});
	this.currentItem = 0;
	
	var card = new Ext.Panel({
			layout:'card'
		,	id: 'dwc-card'
		, 	iconCls: 'icon_events'
		,	region: 'center'
		,	activeItem: this.currentItem
		,	layoutConfig: {
				deferredRender: false
			}
		,	border: false
		,	defaults: {
				border:false
			}
		,	tbar: [{
					text: 'Add Species'
				,	id: 'btn-add-species'
				,	iconCls: 'icon_events_add'
				,	scope: this
				,	handler: this.addSpecimen
			}, {
					text: 'My Species'
				,	id: 'my_species'
				,	iconCls: 'icon_my'
				,	enableToggle: true
				,	listeners: {
						toggle: this.mySpecies.createDelegate( this )
					}
			}, {
					text: 'Remove Species'
				,	id: 'btn-delete-species'
				,	scope: this
				,	disabled: true
				, 	iconCls: 'icon_events_delete'
				,	handler: function(){
						var record = this.grid.getSelectionModel().getSelected();
						if(!Ext.isEmpty(record) || Ext.isDefined(record)){
							this.grid.removeRequest(record.data);
						}else{
							Ext.Msg.alert('Notice', 'Please select species.');
						}
					}
			}, {
					id: 'view-dwc-grid'
				, 	iconCls: 'icon_events_grid'
				,	text: 'Grid View'
				,	handler: this.navHandler.createDelegate(this, ['grid'])
				,	disabled: true
			}, {
					id: 'view-dwc-matrix'
				, 	iconCls: 'icon_events_matrix'
				,	text: 'Matrix View'
				,	handler: this.navHandler.createDelegate(this, ['matrix'])
			}]
		,	items: [
					this.grid
				,	this.matrix
			]
	});

	Ext.apply(this, config, {
			layout: 'border'
		,	id: 'dwc-panel'
		,	hideMode: 'offsets'
		,	items: [
					card	
			]
	});

	SLM.DwcPanel.superclass.constructor.apply(this, arguments);

};

Ext.extend( SLM.DwcPanel, Ext.Panel, {
		
		navHandler: function( view ) {
	
			switch( view ) {
				case 'matrix':
					Ext.getCmp('dwc-card').getLayout().setActiveItem(1);
					Ext.getCmp('view-dwc-grid').enable();
					Ext.getCmp('view-dwc-matrix').disable();
					Ext.getCmp('btn-add-species').disable();
					Ext.getCmp('my_species').disable();
					if (Ext.getCmp('btn-delete-species')) {
						Ext.getCmp('btn-delete-species').disable();
					}
					this.matrix.getStore().reload();
					break;
				case 'grid':
					Ext.getCmp('dwc-card').getLayout().setActiveItem(0);
					Ext.getCmp('btn-add-species').enable();
					Ext.getCmp('my_species').enable();
					Ext.getCmp('view-dwc-matrix').enable();
					this.grid.enableDisbleEvent();
					Ext.getCmp('view-dwc-grid').disable();
					break;
			}
			
		}

	,	addEvent: function() {
			var addEvent = new SLM.Event({'cmd':'add'});
			addEvent.on('saveSuccessful', function() {
				this.getStore.reload();	
			});
			addEvent.show();
		}

	,	addSpecimen: function() {
			var addSpecies = new SLM.DwcAdd();
			addSpecies.on('close', function() {
				this.grid.getStore().reload();															
			}, this);
			addSpecies.show();
		}
		
	,	mySpecies: function( btn, toggle ) {
			if ( toggle ) {
				this.my_species = true;
			} else {
				this.my_species = false;
			}
			this.grid.store.load({ params: { start:0, limit: 100 } });
		}
});